package com.hxgz.steel.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.*;
import com.hxgz.steel.domain.*;
import com.hxgz.steel.dto.*;
import com.hxgz.steel.service.NewOrderService;
import com.hxgz.steel.service.SellerOrderService;
import com.hxgz.steel.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 卖家订单
 */
@Service
@Transactional
public class SellerOrderServiceImpl implements SellerOrderService {

    @Autowired
    OrderMapper oMapper;
    @Autowired
    AskInfoMapper aMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    QuotationMapper quotationMapper;
    @Autowired
    AskInfoMapper askInfoMapper;
    @Autowired
    ProductCommentsMapper productCommentsMapper;
    @Autowired
    UserService uService;
    @Autowired
    private NewOrderService newOrderService;

    /**
     * 列表
     *
     * @param dto
     * @param page
     * @return
     */
    @Override
    public ServerResponse listSellerOrders(SellerOrderDto dto, Page page, Long userId) {
        PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
        //模拟一次sql查询
        Map map = new HashMap();
        map.put("status", dto.getStatus());
        map.put("timeStr", dto.getPayMentTimeStr());
        map.put("timeEnd", dto.getPayMentTimeEnd());
        map.put("userId", userId);
        List<SellerOrderDto> list = oMapper.getListByTimeAndStatus(map);
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 查看详情
     *
     * @param id     订单id
     * @param userId
     * @return
     */
    @Override
    public ServerResponse getOrderInfo(Long id, Long userId) {
        return newOrderService.getOrderDetailSeller(id,userId);
    }


    /**
     * 填写物流单号
     *
     * @param id
     * @param logistics
     * @param logisticsNum
     * @return
     */
    @Override
    public ServerResponse writeLogisticsNum(Long id, String logistics, String logisticsNum) {
        Order order = new Order();
        order.setId(id);
        order.setStatus(2);
        order.setStatusSeller(2);
        order.setLogistics(logistics);
        order.setLogisticsNum(logisticsNum);
        order.setSendTime(new Date());
        return oMapper.updateByPrimaryKeySelective(order) == 1 ? ServerResponse.createBySuccessMessage("发货成功") : ServerResponse.createByErrorMessage("操作失败");
    }

    @Override
    public ServerResponse WXlistSellerOrders(Integer status, Long userId, Page page) {
        PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
        List<WXorderDto> list = oMapper.WXlistSellerOrders(status, userId);
        for (WXorderDto dto : list) {
            //null != dto.getQuotationId()表示为求购订单
            if (null != dto.getQuotationId()) {
                dto.setList(newOrderService.newGetOrderItemByOrderId(dto.getId(),dto.getQuotationId()));
            }
        }
//        for (WXorderDto dto : list){
//            //null != dto.getQuotationId()表示为求购订单
//            if(null != dto.getQuotationId()){
//                dto.setList( orderItemMapper.getOrderItemByOrderId(dto.getId()) );
//            }
//        }
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse wxBuySellerOrders(Integer status, Long userId, Page page) {
        PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
        List<WxSelfBuyOrderDto> list = oMapper.wxBuySellerOrders(status, userId);
        PageInfo pageInfo = new PageInfo(list);
        if (CollectionUtils.isEmpty(list)) {
            return ServerResponse.createBySuccess(pageInfo);
        }
        List<WxAskBuyOrderListDto> askList = oMapper.wxBuyListSellerOrders(status, userId);
        for (WxSelfBuyOrderDto wxSelfBuyOrderDto : list) {
            List<WxAskBuyOrderListDto> newList = new ArrayList<>();
            for (WxAskBuyOrderListDto wxAskBuyOrderListDto : askList) {
                if (wxSelfBuyOrderDto.getOrderId().equals(wxAskBuyOrderListDto.getOrderId())) {
                    newList.add(wxAskBuyOrderListDto);
                }
            }
            wxSelfBuyOrderDto.setWxAskBuyOrderListDtos(newList);
        }

        return ServerResponse.createBySuccess(pageInfo);
    }

    @Override
    public ServerResponse logisticsCompanyListChange() {
        try {
            List<LogisticsCompanyDropDto> list = oMapper.getLogisticsCompanyList();
            return ServerResponse.createBySuccess(list);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public ServerResponse wxOrderInformation(Long userId, Long orderId) {
        //TODO 预留接口
        Integer status = null;
        try {
            Order order = oMapper.selectByPrimaryKeyAnduserId(orderId, userId);
            if (order == null) {
                return ServerResponse.createByErrorMessage("非法请求");
            }
            WxOrderInformationDto wxOrderInformationDto = oMapper.getWxOrderInformationByOrderId(orderId);
            List<WxAskBuyOrderListDto> list = oMapper.getWxAskBuyOrderListByUserId(userId, status);
            List<WxAskBuyOrderListDto> newList = new ArrayList<>();
            for (WxAskBuyOrderListDto wxAskBuyOrderListDto : list) {
                if (orderId.equals(wxAskBuyOrderListDto.getOrderId())) {
                    newList.add(wxAskBuyOrderListDto);
                }
            }
            wxOrderInformationDto.setWxAskBuyOrderListDtos(newList);
            if (new Integer(2).equals(wxOrderInformationDto.getStatus()) || new Integer(3).equals(wxOrderInformationDto.getStatus())) {

            }
            return ServerResponse.createBySuccess(wxOrderInformationDto);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public ServerResponse addProductComment(Long orderId, Long userId, String comment, Integer grade) {
        try {
            Order order = oMapper.getOrderByUserIdAndOrderId(userId, orderId);
            if (order.getQuotationId() != null && !"".equals(order.getQuotationId())) {
                return ServerResponse.createByErrorMessage("禁止评论");
            }
            ProductComments productComments = productCommentsMapper.getProduct(orderId);
            if (order == null) {
                return ServerResponse.createByErrorMessage("用户没有该订单");
            }
            if (productComments != null) {
                return ServerResponse.createByErrorMessage("该订单已被评价");
            }
            OrderItem orderItem = orderItemMapper.getOrderItemIDByOrderId(orderId);
            ProductComments newProductComments = new ProductComments();
            newProductComments.setOrderItemId(orderItem.getId());
            newProductComments.setComment(comment);
            newProductComments.setGrade(grade);
            int productSuccess = productCommentsMapper.insertSelective(newProductComments);
            return productSuccess > 0 ? ServerResponse.createBySuccessMessage("评价成功") : ServerResponse.createByErrorMessage("评价失败");
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public ServerResponse showProductComment(Long orderId, Long userId) {
        try {
            Order order = oMapper.getOrderByUserIdAndOrderId(userId, orderId);
            if (order == null) {
                return ServerResponse.createByErrorMessage("用户没有该订单");
            }
            if (order.getQuotationId() != null && !"".equals(order.getQuotationId())) {
                return ServerResponse.createByErrorMessage("禁止评论");
            }
            ProductComments productComments = productCommentsMapper.getProduct(orderId);
            return ServerResponse.createBySuccess(productComments == null ? new ProductComments() : productComments);
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 商城自营订单明细
     *
     * @param userId
     * @param orderId
     * @return
     */
    @Override
    public ServerResponse wxSelfOrderInformation(Long userId, Long orderId) {
        return newOrderService.getOrderDetailMiniPrograms(userId,orderId);
    }

}
